<?php 
namespace app\index\controller;
use think\Controller;
use think\Request;
use app\index\model\User as UserModel;

header("content-type:text/html;charset=utf-8");
class User extends Controller
{
	
    // 用户列表
    public function show(){

    	$user = new UserModel;
    	$array = $user->show();
    	$this->assign('data',$array);
    	return view('show');
    }

    // 用户删除
    public function delete()
    {
        $Request = Request::instance();
        $id = $Request->get('id');
        // var_dump($id);die;
        $user = new UserModel;
        $result = $user->deleteData($id);
        if($result)
        {
            // return Redirect('Index/show');
            $this->success('删除成功！','User/show');
        }
        else
        {
            // echo "删除失败！";
            $this->error('删除失败！');
        }

    }

    // 用户修改
    public function update()
    {
	    $Request=Request::instance();
	    $id=$Request->get('id');
	    $user=new UserModel;
	    $res=$user->findData($id);//查询单条
	    //print_r($res);die;
	    return view('update',['res'=>$res]);
    }

    public function save()
    {
	    $Request = Request::instance();
	    $id = $Request->post('id');
        // var_dump($id);die;
	    $Request = Request::instance();
	    $data = $Request->post();//接修改之后的数据
	    $user = new UserModel;
	    $result = $user->updateData($data,$id);//修该语句
	    if($result)
	    {
	    	// return Redirect('user/show');
            $this->success('修改成功','user/show');
	    }
	    else
	    {
	        $this->error('修改失败');
	    }
    }

    // 添加用户
    public function insert()
    {
        $Request = Request::instance();
        $data = $Request->post();
        // var_dump($data);die;
        if($data)
        {
            $model = new UserModel;
            $sql = $model->insertData($data);
            if($sql)
            {
                $this->success('添加成功','user/show');
            }
            else
            {
                $this->error('添加失败');
            }
        }
    
        return view();
    }

}